from django.shortcuts import render
from django.http import HttpResponse,HttpResponseRedirect
from .models import *
from login.models import PersonID,PersonSf
from .face_ai_baidu import *
from django.urls import reverse

#from .人脸识别55 import ComparFace

# Create your views here.

def face_add(request):
    post_info = request.POST
    #print(post_info)
    '''
    post_user_id = post_info['id']
            
    post_image = post_info['xx'].replace('无效data:image/jpeg;base64,','')#64编码
        
    #print(post_info)
    #print(type(post_image))
        
    user_obj = PersonID.objects.get(pk = post_user_id)
    print(user_obj)
    print(user_obj.id)
    FaceImage.objects.create(image = post_image,
                             person_id = user_obj)
    x = AddFace(img_64 = json.dumps(post_image)).add_face_to_ku(
    post_user_id,user_obj.person_name)
    print(x)
    context = {}
    return render(request,'login/denglu.html',context)
    '''
    try:
        post_user_id = post_info['id']
            
        post_image = post_info['xx'].replace('data:image/jpeg;base64,','')#64编码
        print(post_info['xx'])
        #print(post_info)
        #print(type(post_image))
        
        user_obj = PersonID.objects.get(pk = post_user_id)
        
        FaceImage.objects.create(image = post_info['xx'],
                             person_id = user_obj)
        x = AddFace(img_64 = post_image).add_face_to_ku(
        post_user_id,user_obj.person_name)
        print(x)
        print(6)
        context = {}
        return render(request,'login/denglu.html',context)
    
    except KeyError:
        post_image = post_info['xx'].replace('data:image/jpeg;base64,','')#64编码
        xx = SearchFace(img_64 = post_image).search()
        print(xx)
        if xx['result']['user_list'][0]['score'] >= 80:
            person_name = xx['result']['user_list'][0]['user_info'] #['user_id']
            
            user_id = xx['result']['user_list'][0]['user_id']
            user_sf = PersonID.objects.get(pk = user_id).sf_id
            zhiye = PersonSf.objects.get(pk = user_sf).person_sf
            request.session['权限'] = zhiye
            if request.session['权限'] == '学生':
                request.session['student_name'] = person_name
                print(xx)
                context = {'student_name':person_name}
                return render(request,'paper/student_index.html',context)
            elif request.session['权限'] =='老师':
                request.session['teacher_name'] = person_name
                request.session['zhuce'] = '注册'
                return HttpResponseRedirect(reverse('paper:teacher_index'))
        else:
            return HttpResponse('您还没有注册人脸，快去注册吧！')
    except:
        context = {'error':'拍摄后才能提交哦！'}
        return render(request,'face/face_save.html',context)
    
def face_login(request):
    context={'denglu':'登陆'}
    return render(request,'face/face_save.html',context)


def face_test(request,name_id):
    context={'id':name_id,'zhuce':'注册'}
    return render(request,'face/face_save.html',context)

#人脸查询
def face_demand(request):
    context ={'chaxun':'查询'}
    return render(request,'paper/demand.html',context)

def face_demand2(request):
    post_info = request.POST
    post_image = post_info['xx'].replace('data:image/jpeg;base64,','')#64编码
    face_info = DetectFace(img_64 = post_image).detect_face()
    person_info = SearchFace(img_64 = post_image).search()
    student_name = person_info['result']['user_list'][0]['user_info']
    person = PersonID.objects.get(person_name = student_name)
    person.facedemand_set.create(person_info = person_info)
    person.faceinfo_set.create(face_info = face_info)
    #FaceDemand.objects.create()
    request.session['name_id'] = person.id
    print(face_info)
    print(person_info)
    context ={'ok':'ok'}
    return render(request,'paper/demand.html',context)

#拍照
def demand_face(request):
    context = {}
    return render(request,'paper/face_demand.html',context)

#颜值
def face_detect(request):
    person_id = request.session['name_id']
    print(person_id)
    person_id = PersonID.objects.get(pk = person_id)
    #颜值info
    person_info = person_id.faceinfo_set.all()
    for i in person_info:
        person_list = []
        person_list.append(eval(i.face_info)) #字典格式输出且只输出最后一次的info
    yanzhi = person_list[0]['result']['face_list'][0]['beauty']
    #print(yanzhi)
    age = person_list[0]['result']['face_list'][0]['age']
    context ={'yanzhi':yanzhi,'age':age}
    return render(request,'paper/demand.html',context)
#个人信息
def face_info(request):
    person_id = request.session['name_id']
    print(person_id)
    #学生info
    person_id = PersonID.objects.get(pk = person_id)
    face_info = person_id.facedemand_set.all()
    name = person_id.person_name
    name_id = person_id.sf_id
    zhiye = PersonSf.objects.get(pk = name_id).person_sf
    photo = person_id.faceimage_set.all().first()
    print(photo)
    #print(name)
    #print(name_id)
    context ={'name':name,'zhiye':zhiye,'photo':photo}
    return render(request,'paper/demand.html',context)








def face_post(request):
    x = request.POST
    print(x)
    FaceImage.objects.create(image = x['xx'].replace('无效data:image/jpeg;base64,',''))
    
    return HttpResponse(x)

def face_show(request,pk):
    #image_64 = FaceImage.objects.get(pk = pk)
    image_64 = ImageFace.objects.get(pk = pk)
    
    context = {'image':image_64}

    return render(request,'face/face_show.html',context)
    
def face_add_baidu(request,pk):

    image_64 = FaceImage.objects.get(pk = pk).\
               image.replace('无效data:image/jpeg;base64,','')
    
    post_user_id = 'user1'
    person_name = '花无缺'
    

    x = AddFace(img_64 = image_64).add_face_to_ku(post_user_id,person_name)

    print(x)
    return HttpResponse(str(x))

def face_duibi(request):

    context={}

    return render(request,'face/face_duibi.html',context)

def face_search(request):

    image_64 = request.POST['xx'].replace('data:image/jpeg;base64,','')
    x = SearchFace(img_64 = image_64).search()

    return HttpResponse(str(x))
def face_match(request):
    context={}

    return render(request,'face/face_match.html',context)

def base64_image(img_path):
        '''\
        img_path :图片连接
        return :  返回图片的base64编译字符串
        '''
        import base64
        with open(img_path,'rb') as f:
            base64_data = base64.b64encode(f.read())

        return str(base64_data,'utf-8')

def face_match_yunsuan(request):
    
    img_1 = request.FILES.get('file1')
    img_2 = request.FILES.get('file2')

    img1_obj = ImageFace(image = img_1,image_name = img_1.name)
    img1_obj.save()
    img2_obj = ImageFace(image = img_2,image_name = img_2.name)
    img2_obj.save()

    img_1 = img1_obj.image.name
    
    
    img_2 = img2_obj.image.name

    #img_1 = base64_image(img_1)
    #img_2 = base64_image(img_2)
    x = MatchFace(face1 = img_1,face2 = img_2).match()

    
    
    
    return HttpResponse(str(x))
    



















    
    
    
